#pragma once 

#include "bmppallettemanager.h"
#include "templatearray.h"
#include "bmpzoomview.h"
#include "bmpcliprect.h"
#include "editorpanel.h"
#include "buttonbmpsaverect.h"
#include "icon_res.h"

class SClipPallette;

class BMPEditor : public EditorPanel
{
protected:
	/** manages the potentially big bmp content panel */
	BMPZoomViewPanel m_content;
	/** the bitmap to draw */
	BitMaP * m_bmp;
	/** indexed color pallette */
	BMPPalletteManager m_pallette;
	/** how big the system would like the image to be */
	SPoint m_idealDimensions;
	/** the clips that belong to this image */
	TemplateVectorList<ImageClip> m_bmpClips;
public:
	/** this bmp editor will keep it's own copy of images */
	void addClip(ImageClip & clip)
	{
		printf("addign clip\n");
		m_bmpClips.add(clip);
	}
	/** the image clips that the editor keeps track for itself */
	TemplateVectorList<ImageClip> * getClips(){return &m_bmpClips;}

	/** set the clip palette so that the clip rect can add to it */
	void setClipPallette(SClipPallette * a_clipPallette)
	{
		int index = 0;
		do{
			index = m_toolbar.getToolNamed(BMPClipRect::NAME, index);
			if(index >= 0){
				((BMPClipRect*)m_toolbar.getTool(index))->setClipPallette(a_clipPallette);
				index++;
			}
		}while(index >= 0);
	}
	BMPPalletteManager * getPallette(){return & m_pallette;}
	BitMaP * getBMP(){return m_bmp;}
	void release()
	{
		EditorPanel::release();
		ParentPanel::release();
		if(m_bmp){m_bmp->release();m_bmp=0;}
	}
	~BMPEditor(){release();m_bmpClips.release();}
	BMPEditor():m_bmp(0),m_bmpClips(10)
	{
		addChild(&m_pallette);
		setZoomViewPanel(&m_content);
	}
	void setArea(const SRect & a_area)
	{
		EditorPanel::setArea(a_area);

		// TODO make these values for the palette less arbitrary. TODO make palette more awesome.
		int height = getMasterPanel()->getImageClip(0).clip.getHeight();
		int width = height*2;

		SRect area(*m_zoomViewParent.getArea());
		// setup the pallette area
		SRect palleteArea(a_area.getMaxX()-width, area.getMinY(), width, area.getHeight());
		m_pallette.setArea(palleteArea);
		// take room from the editor area to there is no overlap with the palette
		m_zoomViewParent.getArea()->setMaxX(palleteArea.getMinX());
	}
	void setBMP(BitMaP * a_bmp)
	{
		if(m_bmp){m_bmp->release();}
		m_bmp = a_bmp;
		m_pallette.setBMP(m_bmp);
		m_content.set(m_bmp, m_pallette.getSelectedColorIndex(BMPPalletteManager::COLOR_TRANSPARENT));
		// TODO 3 is the magic number...
		m_content.setZoom(3, SPoint());
		// TODO ensure m_content viewable area is showing...
	}
	void set(BitMaP * a_bmp, ToolBar::ToolData * a_toolData, const int & a_numTools)
	{
		EditorPanel::setTools(a_toolData, a_numTools);
		setBMP(a_bmp);
	}
	void initUI(Panel * a_parent)
	{
		setParent(a_parent);
		MasterPanel * mp = (MasterPanel *)getMasterPanel();
		// where in the tools.bmp image do icons for each tool exist? TODO move to a script?
		ToolBar::ToolData toolData[] = {
			{"Save", mp->getImageClip(ICON_SAVE)},
			{"Load", mp->getImageClip(ICON_REFRESH2)},
			{"Pencil", mp->getImageClip(ICON_PENCIL)},
			{"Eraser", mp->getImageClip(ICON_ERASER)},
			{"Dropper", mp->getImageClip(ICON_EYE_DROPPER)},
			{"ClipRect", mp->getImageClip(ICON_DOTTED_RECT)},
			{"OrderColors", mp->getImageClip(ICON_ORDER_CLR_PAL)},
			{ButtonBMPSaveRect::NAME, mp->getImageClip(ICON_SAVE_RECTS)},
		};
		int numTools = sizeof(toolData)/sizeof(ToolBar::ToolData);
		EditorPanel::setTools(toolData, numTools);
	}

	bool saveMethod(const char * a_path)
	{
		bool saved = getBMP()->save(a_path) == 0;
//		updateTabTextFromPath(a_path);
		return saved;
	}

	virtual bool loadMethod(const char * a_path);

	void saveRectanglesToBuffer(TemplateVectorList<char> * a_buffer)
	{
		const int bufferSize = 1024;
		char buffer[bufferSize];
		int numValues = 0;
		// first, calculate how many numbers are needed
		numValues += m_bmpClips.size()*4; //

		sprintf_s(buffer, "short data[%d] = {\n", numValues);
		a_buffer->add(buffer, strlen(buffer));
		SRect r;
		for(int a = 0; a < m_bmpClips.size(); ++a)
		{
			r = m_bmpClips.get(a).clip;
			sprintf_s(buffer, "%d,\t%d,\t%d,\t%d,\t// %d\n", r.x, r.y, r.w, r.h, a);
			a_buffer->add(buffer, strlen(buffer));
		}
		sprintf_s(buffer, "};\n");
		a_buffer->add(buffer, strlen(buffer));
	}

	bool saveRectanglesMethod(const char * a_fileName)
	{
		// load the sprite file into this buffer
		TemplateVectorList<char> buffer(512);
		saveRectanglesToBuffer(&buffer);
		// save the buffer to the file system
		FILE * pFile;
		fopen_s(&pFile, a_fileName,"w");
		if (pFile!=NULL)
		{
			for(int i = 0; i < buffer.size(); ++i)
			{
				fputc(buffer.get(i),pFile);
			}
			fclose (pFile);
			return true;
		}
		return false;
	}

	bool saveRectangles()
	{
		const int BUFFER_SIZE = 1024;
		char buffer[BUFFER_SIZE];
		fileDialog("Save Rectangles", m_filename, 0, buffer, BUFFER_SIZE, getFileName());
		return (buffer[0] && saveRectanglesMethod(buffer));
	}
};